package org.jeecg.modules.villageCadreAssist.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.util.SecurityUtil;
import org.jeecg.modules.villageCadreAssist.entity.VcaFarmland;
import org.jeecg.modules.villageCadreAssist.entity.VcaFarmlandFee;
import org.jeecg.modules.villageCadreAssist.mapper.VcaFarmlandFeeMapper;
import org.jeecg.modules.villageCadreAssist.service.IVcaFarmlandFeeService;
import org.jeecg.modules.villageCadreAssist.service.IVcaFarmlandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: vca_farmland_fee
 * @Author: jeecg-boot
 * @Date:   2022-04-14
 * @Version: V1.0
 */
@Service
public class VcaFarmlandFeeServiceImpl extends ServiceImpl<VcaFarmlandFeeMapper, VcaFarmlandFee> implements IVcaFarmlandFeeService {

    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private ISysDictItemService sysDictItemService;
    @Autowired
    private IVcaFarmlandService vcaFarmlandService;

    @Override
    public void genData(Integer type, Integer farmlandId) {
        LocalDate today = LocalDate.now();
        int year = today.getYear();

        LoginUser user = SecurityUtil.getLoginUser();
        List<VcaFarmlandFee> list = baseMapper.getUpdateList(type,farmlandId,year,user.getOrgCode());
        if(type==2 || type==3){//删除相关数据
            LambdaQueryWrapper<VcaFarmlandFee> queryWrapper = new LambdaQueryWrapper<VcaFarmlandFee>().eq(VcaFarmlandFee::getOrgCode, user.getOrgCode());
            if(farmlandId != null){
                VcaFarmland farmland = vcaFarmlandService.getById(farmlandId);
                queryWrapper.eq(VcaFarmlandFee::getFarmCode,farmland.getFarmCode());
            }
            super.remove(queryWrapper);
        }
        List<DictModel> items = sysDictService.getDictItems("farmland_fee");
        Map<String, String> fees = items.stream().collect(Collectors.toMap(DictModel::getText, DictModel::getValue));

        Date now = new Date();
        list.forEach(item->{
            item.setYear(year);
            item.setWaterFee(new BigDecimal(fees.get("water_fee")).divide(new BigDecimal("100")).multiply(item.getTillageArea()));
            if(item.getTillageArea().compareTo(new BigDecimal(100))>-1){
                item.setServeFee(new BigDecimal(fees.get("serve_fee")).divide(new BigDecimal("100")).multiply(item.getTillageArea()));
                item.setInsuranceFee(new BigDecimal(fees.get("big_rice_insurance_fee")).divide(new BigDecimal("100")).multiply(item.getTillageArea()));
            }else{
                item.setInsuranceFee(new BigDecimal(fees.get("rice_insurance_fee")).divide(new BigDecimal("100")).multiply(item.getTillageArea()));
                item.setServeFee(BigDecimal.ZERO);
            }
            item.setCreateBy(user.getUsername());
            item.setCreateTime(now);
            item.setStatus(item.getStatus()==null?1:item.getStatus());
            item.setReceiveInsuranceFee(item.getReceiveInsuranceFee()==null?BigDecimal.ZERO:item.getReceiveInsuranceFee());
            item.setReceiveServeFee(item.getReceiveServeFee()==null?BigDecimal.ZERO:item.getReceiveServeFee());
            item.setReceiveWaterFee(item.getReceiveWaterFee()==null?BigDecimal.ZERO:item.getReceiveWaterFee());
        });
        baseMapper.saveBatch(list);
    }

    @Override
    public IPage<VcaFarmlandFee> pageList(Page<VcaFarmlandFee> page, QueryWrapper<VcaFarmlandFee> queryWrapper, String orgCode) {
        return baseMapper.pageList(page, queryWrapper, orgCode);
    }
}
